import axios from 'axios';

const axiosInstance = axios.create({
    baseURL: 'http://localhost:8080', // 根据实际情况设置基础URL
});

axiosInstance.interceptors.request.use(
    (config) => {
        const currentUserString = localStorage.getItem('currentUser');
        if (currentUserString) {
            try {
                const currentUser = JSON.parse(currentUserString);
                const token = currentUser?.token;
                if (token) {
                    config.headers.Authorization = `Bearer ${token}`;
                    console.log('设置 Authorization 头:', `Bearer ${token}`); // 添加调试信息
                } else {
                    console.warn('currentUser 对象中没有 token 属性');
                }
            } catch (error) {
                console.error('解析 currentUser 失败:', error);
            }
        } else {
            console.warn('localStorage 中没有 currentUser 对象');
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

export default axiosInstance;